package mop.bootstrap;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import mop.controller.ChangeClockController;
import mop.controller.CreateProjectController;
import mop.controller.CreateResourceController;
import mop.controller.CreateTaskController;
import mop.controller.CreateUserController;
import mop.controller.FocusWorkController;
import mop.controller.LoginController;
import mop.controller.MakeResourceReservationController;
import mop.controller.ModifyTaskDetailsController;
import mop.controller.RemoveProjectController;
import mop.controller.RemoveTaskController;
import mop.controller.UpdateTaskStatusController;
import mop.controller.UseCaseController;
import mop.model.World;
import mop.security.acl.Acl;
import mop.security.acl.AssetInterface;
import mop.security.acl.Role;
import mop.security.acl.RoleInterface;

/**
 * Class for controller ACL
 * @author MOP Team 7
 */
public class ControllerACL {
	
	public void buildControllers(World world, Acl acl){
		/**
		 * List with controllers
		 */
		LoginController loginc = new LoginController(world);
	
	    //      for the admin...
	    List<UseCaseController> adminControllers = new LinkedList<UseCaseController>();
	    adminControllers.add(new ChangeClockController(world));
	    adminControllers.add(new CreateUserController(world));
	    adminControllers.add(loginc);
	
	    //      for the users...
	    List<UseCaseController> userControllers = new LinkedList<UseCaseController>();
	    userControllers.add(new CreateProjectController(world));
	    userControllers.add(new CreateResourceController(world));
	    userControllers.add(new CreateTaskController(world));
	    userControllers.add(new FocusWorkController(world));
	    userControllers.add(new MakeResourceReservationController(world));
	    userControllers.add(new ModifyTaskDetailsController(world));
	    userControllers.add(new RemoveProjectController(world));
	    userControllers.add(new RemoveTaskController(world));
	    userControllers.add(new UpdateTaskStatusController(world));
	    userControllers.add(loginc);
	   
	    /**
	     * Build ACL
	     */
	    acl.allow(null, loginc, null, null);
	    
	    List<RoleInterface> admin = new LinkedList<RoleInterface>();
	    List<AssetInterface> adminAssets = new LinkedList<AssetInterface>();
	    adminAssets.addAll(adminControllers);
	    admin.add(new Role("Administrator"));
	    acl.allow(admin, adminAssets, null, null);
	    
	    List<RoleInterface> users = new LinkedList<RoleInterface>();
	    List<AssetInterface> userAssets = new LinkedList<AssetInterface>();
	    userAssets.addAll(userControllers);
	    users.addAll(world.getUserTypeDataBase().fetchAll());
	    acl.allow(users, userAssets, null, null);
	    
	    /**
	     * Add to world
	     */
        Set<UseCaseController> controllers = new HashSet<UseCaseController>();
        controllers.addAll(userControllers);
        controllers.addAll(adminControllers);
        world.setControllers(controllers);
	    
	}
    
}
